﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TunnelManager.Tools;

namespace TunnelManager.UserManager
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void btOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbUserName.Text) || string.IsNullOrEmpty(tbPassword.Text))
            {
                MessageBox.Show("用户名或密码不能为空", "登录失败", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
            }
            else
            {
                SqlDBHelper sdh = new SqlDBHelper();
                string loginSql = "select top 1 * from M_User where UserID='" + tbUserName.Text + "' and Password='" + tbPassword.Text + "' and isWorking='1'";
                DataTable loginDT = sdh.GetTable(loginSql);
                if (loginDT.Rows.Count != 0)
                {
                    User.Username = loginDT.Rows[0]["Name"].ToString();
                    MessageBox.Show("用户[" + User.Username + "]登陆成功", "欢迎", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    User.Userid = loginDT.Rows[0]["UserID"].ToString();                    
                    User.Role = loginDT.Rows[0]["RoleID"].ToString();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("登录失败，请检查您的用户名和密码", "登录失败", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    User.Userid = "00000";
                    User.Username = "未登录";
                    User.Role = "99";
                }
            }
        }
    }
}
